<?php

namespace app\model;

use app\BaseModel;
use think\model\relation\BelongsTo;
use think\model\relation\HasMany;

/**
 * 账号
 */
class UserModel extends BaseModel
{
    protected $name = 'user';

    /**
     * 新增数据验证
     * @return array
     */
    public function addValidate(): array
    {
        return [
            'username|账号'  =>  'require|length:8,18|alphaNum',
            'password|密码'  =>  'require|length:8,18',
            'name|姓名' =>  'require',
//            'age|年龄' =>  'require',
            'sex|性别' =>  'require',
            'role_id|角色' =>  'require',
        ];
    }

    /**
     * 账号登录数据验证
     * @return array
     */
    public function usernameLoginValidate(): array
    {
        return [
            'username|账号' => 'require',
            'password|密码' => 'require',
        ];
    }

    /**
     * 手机登录数据验证
     * @return array
     */
    public function phoneLoginValidate(): array
    {
        return [
            'phone|手机号码' => 'require',
            'code|手机验证码' => 'require',
        ];
    }

//    public function editValidate(): array
//    {
//        return [
//            'user_number|编号' => 'checkUserNumber',
//        ];
//    }
//
//    protected function checkUserNumber($value,$rule,$data): bool|string
//    {
//        return $rule == $value ? true : '名称错误';
//    }

    /**
     * 关联部门
     * @return BelongsTo
     */
    public function department(): BelongsTo
    {
        return $this->belongsTo(DepartmentModel::class,'department_id')->bind([
            'department_name'=>'name',
            'departmentAdmins',
        ]);
    }

    /**
     * 关联部门-部门名称
     * @return BelongsTo
     */
    public function departmentName(): BelongsTo
    {
        return $this->belongsTo(DepartmentModel::class,'department_id')->bind(['department_name'=>'name']);
    }

    /**
     * 关联部门-部门名称,学制模板
     * @return BelongsTo
     */
    public function departmentNameMode(): BelongsTo
    {
        return $this->belongsTo(DepartmentModel::class,'department_id')->bind(['department_name'=>'name','department_stage'=>'stage','department_mode'=>'mode','mode_name','level_rules']);
    }

    /**
     * 关联单位-单位名称
     * @return BelongsTo
     */
    public function unitName(): BelongsTo
    {
        return $this->belongsTo(UnitModel::class,'unit_id')->bind(['unit_name'=>'name']);
    }

    /**
     * 关联公司-公司名称
     * @return BelongsTo
     */
    public function companyName(): BelongsTo
    {
        return $this->belongsTo(CompanyModel::class,'company_id')->bind(['company_name'=>'name']);
    }

    /**
     * 关联角色-角色名称
     * @return BelongsTo
     */
    public function roleName(): BelongsTo
    {
        return $this->belongsTo(RoleModel::class,'role_id')->bind(['role_name'=>'name']);
    }


    /**
     * 关联标签关系表
     * @return HasMany
     */
    public function userTag(): HasMany
    {
        return $this->hasMany(UserTagModel::class,'uid');
    }

    /**
     * 管理部门
     * @return HasMany
     */
    public function DepartmentAdmins(): HasMany
    {
        return $this->hasMany(DepartmentAdminModel::class,'uid');
    }

    /**
     * 咨询师排班
     * @return HasMany
     */
    public function ConsultTime(): HasMany
    {
        return $this->hasMany(ConsultTimeModel::class,'consult_id');
    }

}